package com.cencat.driver.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * 定时任务配置类
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Configuration
@EnableScheduling
@ConfigurationProperties(prefix = "scheduled")
public class ScheduledConfig {

    /**
     * 线程池大小
     */
    private int poolSize = 10;

    /**
     * 任务配置
     */
    private TasksProperties tasks = new TasksProperties();

    /**
     * 定时任务线程池
     */
    @Bean("taskScheduler")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(poolSize);
        scheduler.setThreadNamePrefix("cencat-driver-scheduled-");
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setAwaitTerminationSeconds(60);
        scheduler.setRejectedExecutionHandler((r, executor) -> {
            // 记录被拒绝的任务
            System.err.println("定时任务被拒绝执行: " + r.toString());
        });
        return scheduler;
    }

    /**
     * 任务配置属性
     */
    public static class TasksProperties {
        
        /**
         * 清理过期消息任务
         */
        private TaskConfig cleanExpiredMessages = new TaskConfig(true, "0 0 2 * * ?");
        
        /**
         * 处理超时任务
         */
        private TaskConfig handleTimeoutTasks = new TaskConfig(true, "0 */5 * * * ?");
        
        /**
         * 更新司机在线状态
         */
        private TaskConfig updateDriverStatus = new TaskConfig(true, "0 */1 * * * ?");
        
        /**
         * 车辆状态检查
         */
        private TaskConfig checkVehicleStatus = new TaskConfig(true, "0 0 */1 * * ?");

        public TaskConfig getCleanExpiredMessages() {
            return cleanExpiredMessages;
        }

        public void setCleanExpiredMessages(TaskConfig cleanExpiredMessages) {
            this.cleanExpiredMessages = cleanExpiredMessages;
        }

        public TaskConfig getHandleTimeoutTasks() {
            return handleTimeoutTasks;
        }

        public void setHandleTimeoutTasks(TaskConfig handleTimeoutTasks) {
            this.handleTimeoutTasks = handleTimeoutTasks;
        }

        public TaskConfig getUpdateDriverStatus() {
            return updateDriverStatus;
        }

        public void setUpdateDriverStatus(TaskConfig updateDriverStatus) {
            this.updateDriverStatus = updateDriverStatus;
        }

        public TaskConfig getCheckVehicleStatus() {
            return checkVehicleStatus;
        }

        public void setCheckVehicleStatus(TaskConfig checkVehicleStatus) {
            this.checkVehicleStatus = checkVehicleStatus;
        }
    }

    /**
     * 单个任务配置
     */
    public static class TaskConfig {
        
        /**
         * 是否启用
         */
        private boolean enabled;
        
        /**
         * Cron表达式
         */
        private String cron;

        public TaskConfig() {
        }

        public TaskConfig(boolean enabled, String cron) {
            this.enabled = enabled;
            this.cron = cron;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getCron() {
            return cron;
        }

        public void setCron(String cron) {
            this.cron = cron;
        }
    }

    public int getPoolSize() {
        return poolSize;
    }

    public void setPoolSize(int poolSize) {
        this.poolSize = poolSize;
    }

    public TasksProperties getTasks() {
        return tasks;
    }

    public void setTasks(TasksProperties tasks) {
        this.tasks = tasks;
    }
}